//
//  MYHomeViewController.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/19.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SVProgressHUD
import SnapKit


private let HomeCellId: String = "HomeCellId"
private let width = UIScreenWidth - 120


class MYHomeViewController: MYBasicTableViewController {
    
    //下拉加载视图
    let refreshContrl: MYRefreshControl = MYRefreshControl()

    //视图模型: statusListViewModel -> 负责加载数据
    lazy var statusListViewModel = MYStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置homeView的访客页面信息,如果加载了访客视图,就不执行tableView相关的代码
        //MARK: 未登录的情况下tableView为nil
        if !userLogin {
            
            visitorView.upDateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
            return
        }
        
        //准备tableview
        prepareForTableView()
        
        //设置导航条右边的Item
        setRightItem()
        
        //加载数据
        loadData()
        
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        
        SVProgressHUD.dismiss()
    }
    
    //准备tableview
    private func prepareForTableView() {
        //注册cell
        tableView.registerClass(MYStatusCell.self, forCellReuseIdentifier: HomeCellId)
        //自适应行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView.estimatedRowHeight = 200
        //取消cell分割线
        tableView.separatorStyle = .None
        
        self.tableView.addSubview(refreshContrl)
        refreshContrl.addTarget(self, action: #selector(MYHomeViewController.loadData), forControlEvents: .ValueChanged)
        //指定菊花控件为tableview的footerView
        tableView.tableFooterView = pullupView
        
        
    }
    
    
    
    
    
    
    //懒加载菊花控件
    private lazy var pullupView: UIActivityIndicatorView = {
        
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        
        return indicatorView
    }()
    
    
    
    
    //设置右边扫描二维码的Item
    private func setRightItem() {
        
        let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(MYHomeViewController.pushToDetaiVc))
        
        navigationItem.rightBarButtonItem = popItem

    }
    
    
    
    @objc private func pushToDetaiVc() {
        
        let detaiVc = MYDetaiViewController()
        
        detaiVc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(detaiVc, animated: true)
    }
    
    
    
}


//MARK:网络相关
extension MYHomeViewController {
    
    @objc private func loadData() {
        
        statusListViewModel.loadData(pullupView.isAnimating()) { (isSuccess) in
            //停止小菊花
            self.pullupView.stopAnimating()
            //改变状态
            self.refreshContrl.endRefresh()
            //请求失败,显示网络错误提示
            if isSuccess == false {
                SVProgressHUD.showWithStatus(APPErrorTip)
                return
            }
            
            //刷新数据
            self.tableView.reloadData()
        }
    }
    
}



//MARK:数据源方法
extension MYHomeViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.statusListViewModel.statusViewModelArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! MYStatusCell
        
        //上拉静默加载数据的墨迹代码
        if indexPath.row == self.statusListViewModel.statusViewModelArray.count - 1 && !pullupView.isAnimating() {
            
            pullupView.startAnimating()
            //加载数据
            loadData()
        }
        
        
        //给cell的viewmodel属性赋值(同时给cell的子控件赋值)
        cell.statusViewModel = self.statusListViewModel.statusViewModelArray[indexPath.row]
        return cell
    }
    
    
    

}
